﻿using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.SessionState;

namespace MS.Utility.Common
{
    /// <summary>
    /// 单元测试帮助类
    /// </summary>
    public class TestHelper
    {
        /// <summary>
        /// 用Moq构建一个HttpContextBase
        /// </summary>
        /// <returns></returns>
        public static HttpContextBase FakeHttpContextBase()
        {
            var context = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            var response = new Mock<HttpResponseBase>();
            var cookie = new HttpCookieCollection();
            response.SetupGet(x => x.Cookies).Returns(cookie);
            request.SetupGet(x => x.Cookies).Returns(cookie);
            var session = new Mock<HttpSessionStateBase>();
            var server = new Mock<HttpServerUtilityBase>();
            // Our identity additions ...
            var user = new Mock<IPrincipal>();
            var identity = System.Threading.Thread.CurrentPrincipal.Identity;

            context.Setup(ctx => ctx.Request).Returns(request.Object);
            context.Setup(ctx => ctx.Response).Returns(response.Object);
            context.Setup(ctx => ctx.Session).Returns(session.Object);
            context.Setup(ctx => ctx.Server).Returns(server.Object);
            context.Setup(ctx => ctx.User).Returns(user.Object);
            context.Setup(ctx => ctx.User.Identity).Returns(identity);
 
            return context.Object;
        }

        /// <summary>
        /// 构建一个HttpContext
        /// </summary>
        /// <returns></returns>
        public static HttpContext FakeHttpContext()
        {
            return new HttpContext(new HttpRequest("", "http://localhost", ""),
                                                  new HttpResponse(new StringWriter(new StringBuilder())));
   
        }
    }
 
}
